package com.ltmonitor.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.text.StrMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.stat.TableStat.Mode;
import com.ltmonitor.adas.entity.AdasAlarm;
import com.ltmonitor.adas.entity.AdasAttachment;
import com.ltmonitor.dao.IAlarmConfigDao;
import com.ltmonitor.entity.AlarmConfig;
import com.ltmonitor.service.IAdasAlarmService;
import com.ltmonitor.service.IAdasAttachmentService;
import com.ltmonitor.service.IAlarmConfigService;
import com.sun.tools.classfile.Annotation.element_value;

import net.sf.json.JSONObject;

/**
 * 报警信息查看控制器
 * 
 * @author shinion
 *
 */
@Controller
@RequestMapping("/alarmImage")
public class AlarmImageAction extends PaginateAction {

	// 用于查找alarmNo
	@Autowired
	private IAdasAlarmService adasAlarmService;

	// 用于查找图片资源
	@Autowired
	private IAdasAttachmentService adasAttachmentService;
	
	//用于查找报警类型
	@Autowired
	private IAlarmConfigService alarmConfigService;

	/**
	 * 查看报警图片页面，跟据报警编号查询，但是现在alarm表格中的alarmId和adasAlarm中的alarmId不是同一数据，所以该方法返回数据不对
	 * <p> model 中填充数据：</p>
	 * <p> message，填充基本信息 ,adasAlarm，adas报警信息</p>
	 * <p>list，返回的信息数据；num,信息数据的条数</p>
	 * <p> vehicleId，车辆Id；alarmId,报警编号 </p>
	 * <p> alarmName，报警类型； </p>
	 * @param alarmId 警报id
	 * @param model
	 * @return
	 */
	@RequestMapping("/viewAlarmImage.action")
	public String viewAlarmImage(long vehicleId, @RequestParam(defaultValue = "0") long alarmId, Model m) {
		AdasAlarm adasAlarm = null;
		String strMsg = "加载报警资源完成!";
		try {
			// 根据alarmId获取alarmNo，然后查询attachmeng表格获取报警图片等信息
			adasAlarm = adasAlarmService.find("from AdasAlarm where alarmId = ? and status='uploaded'", (int)alarmId);
			if (adasAlarm != null) {
				m.addAttribute("adasAlarm", adasAlarm);
				
				// 根据alarmNo报警附件数据
				List<AdasAttachment> list = adasAttachmentService.query("from AdasAttachment where alarmNo=?",
						adasAlarm.getAlarmNo());
				// 填充数据
				if (list.size() > 0) {
					m.addAttribute("num", list.size());
					m.addAttribute("list", list);
				} else {
					m.addAttribute("num", 0);
					strMsg="当前报警没有多媒体附件";

				}
			} else {
				m.addAttribute("num", 0);
				strMsg="当前表中没有该警报的adas报警信息";
			}
		} catch (Exception ex) {
//			log.error(ex.getMessage(), ex);
			strMsg=",runError:"+ex.getMessage();
			ex.printStackTrace();
			
		}finally {
			m.addAttribute("message",strMsg);
		}
		m.addAttribute("alarmId", alarmId);
		m.addAttribute("vehicleId", vehicleId);
		return "alarm/alarmImgView";
	}
	
	
	/**
	 * <p>  根据alarmNo获取资源信息并返回到页面 </p>
	 *<p>返回数据：1.返回adasAlarm报警信息</p>
	 *<p> 2.返回adasAttachment附件信息</p>
	 * @param alarmNo
	 * @param model
	 * @return
	 */
	@RequestMapping("/viewAdasAlarmImage.action")
	public String viewAdasAlarmImage(@RequestParam(defaultValue="") String alarmNo ,Model model) {
		String strMsg = "加载报警资源完成!";
		try {
			//获取adas报警信息
			AdasAlarm adasAlarm =null;
			adasAlarm = adasAlarmService.find("from AdasAlarm where alarmNo = ?", alarmNo);
			if(adasAlarm != null) {
				model.addAttribute("adasAlarm", adasAlarm);
			}
			// 根据alarmNo报警附件数据
			List<AdasAttachment> list = adasAttachmentService.query("from AdasAttachment where alarmNo=?",
					alarmNo);
			// 填充数据
			if (list.size() > 0) {
				model.addAttribute("num", list.size());
				model.addAttribute("list", list);
			} else {
				model.addAttribute("num", 0);
				strMsg="当前报警没有多媒体附件";

			}
		} catch (Exception ex) {
//			log.error(ex.getMessage(), ex);
			strMsg=",runError:"+ex.getMessage();
			ex.printStackTrace();
			
		}finally {
			model.addAttribute("message",strMsg);
		}
		
		return "alarm/alarmImgView";
	}
	
	
	/**
	 * 查询主数据表：AdasAttachment，对应页面，主动安全报警详细信息查看页面
	 * 根据alarmNo获取当条记录的所有附件信息
	 * @param alarmNo
	 * @return
	 */
	@RequestMapping("/getAdasAlarmImgsData.action")
	public @ResponseBody List getAdasAlarmImgsData(@RequestBody String jsonStr) {
		List<AdasAttachment> list = null;
		try {
			Map<String, Object> map = JSONObject.fromObject(jsonStr);
			String alarmNo = map.get("alarmNo").toString();
			list  = adasAttachmentService.query("from AdasAttachment where alarmNo=?",
					alarmNo);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return list;
	}
	
	
	
	/**
	 * 查询主表格：AdasAttachment，对应页面：主动安全报警详细信息查看页面
	 * 获取单条adas报警中所有记录的附件信息
	 * @param jsonStr
	 * @return
	 */
	@RequestMapping("/getOneAdasAttachments.action")
	public @ResponseBody List getOneAdasAttachments (@RequestBody String jsonStr) {
		List  list = null;
		try {
			Map<String, Object> map = JSONObject.fromObject(jsonStr);
			String queryId = "activeSafeAlarm.selectOneAdasAttachments";
			list = queryService.query(queryId, map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return list;
	}
	
	
	/**
	 * 测试页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/test.action")
	public String testInfo(@RequestParam(value = "vehicleId", defaultValue="0")  long vehicleId,Model model) {
		model.addAttribute("vehicleId",vehicleId);
		model.addAttribute("message", "aaa");
		
		return "video/realTimeVideoNew";
	}
	
	/**
	 * 获取实时视频相关的车辆信息
	 * @param jsonStr
	 * @return
	 */
	@RequestMapping("/getVehicleInfo.action")
	public @ResponseBody List getVehicleInfo(@RequestBody String jsonStr) {
		List list = null;
		try {
			Map<String, Object> map = JSONObject.fromObject(jsonStr);
			String queryId = "vehicle.getRealTimeVideoVehicleInfo";
			list = queryService.query(queryId, map);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			list = new ArrayList();
			list.add(e.getMessage());
		}
		return list;
	}
	
}
